/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucm;

import java.sql.ResultSet;
import sql.Select;

/**
 *
 * @author 马凯
 */
public class CheckRight_UC {

    private static CheckRight_UC cruc;
    private String receive;
    private String output = "";

    public static CheckRight_UC getInstance(String receive) {
        if (cruc == null) {
            cruc = new CheckRight_UC();
            cruc.receive = receive;
        }
        else
        {
            cruc.receive = receive;
        }
        return cruc;
    }

    public String run() {
        String[] str = receive.split(";");
        String cid = str[0];
        String psw = str[1];
        String rollGroup;
        String specifiedGroup;

        try {
        String query = "SELECT roll.groups  FROM rfid, roll WHERE roll.card_id='"+cid+"'AND rfid.cid='"+cid+"' AND rfid.password='"+psw+"'";
        ResultSet rs1 = Select.getInstance().selectValue(query);
        if(rs1.next())
        {
        System.out.println(rs1.getString("groups"));
        rollGroup=rs1.getString("groups");
            if (rs1.first()) {
                if(Convertor.getInstance().rollGroup(Integer.valueOf(rollGroup)).equalsIgnoreCase("Staff"))
                {
                    query = "SELECT groups FROM staff WHERE staff_card='"+cid+"'";
                    ResultSet rs2 = Select.getInstance().selectValue(query);
                    if (!rs2.first()) {
                        output = "N;-1;-1;04";
                    }
                    else
                    {
                        specifiedGroup = rs2.getString("groups");
                        output = "Y;"+rollGroup+";"+specifiedGroup+";04";
                    }
                }
                if(Convertor.getInstance().rollGroup(Integer.valueOf(rollGroup)).equalsIgnoreCase("Student"))
                {
                    query = "SELECT groups FROM student WHERE student_card='"+cid+"'";
                    ResultSet rs2 = Select.getInstance().selectValue(query);
                    if (!rs2.first()) {
                        output = "N;-1;-1;04";
                    }
                    else
                    {
                        specifiedGroup = rs2.getString("groups");
                        output = "Y;"+rollGroup+";"+specifiedGroup+";04";
                    }
                }
                if(Convertor.getInstance().rollGroup(Integer.valueOf(rollGroup)).equalsIgnoreCase("Teacher"))
                {
                    query = "SELECT groups FROM teacher WHERE teacher_card='"+cid+"'";
                    ResultSet rs2 = Select.getInstance().selectValue(query);
                    if (!rs2.first()) {
                        output = "N;-1;-1;04";
                    }
                    else
                    {
                        specifiedGroup = rs2.getString("groups");
                        output = "Y;"+rollGroup+";"+specifiedGroup+";04";
                    }
                }

            } else {
                output = "N;-1;-1;04";
            }
        }
        } catch (Exception e) {
            System.out.println("66776677");
            e.printStackTrace();
        }
        return output;
    }
}
